package com.fire.control.system.module.role.dao;

import java.util.List;
import java.util.Set;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.fire.control.system.dto.SelectRoleDTO;
import com.fire.control.system.entity.Resource;
import com.fire.control.system.entity.Role;

public interface RoleMapper{
	
	@Select("select * from t_roles where id = #{roleId}")
	public Role findById(String roleId);
	
	@Select("SELECT r.id,r.role_name AS name FROM t_roles r WHERE r.role_name IN ('单位管理员','安全管理员','安全巡检员')")
	public List<SelectRoleDTO> getSelectRoles();
	
	/**
	 * @author: xiechengguang 
	 * @Title: getUsersByMsgPushId   
	 * @Description: 通过消息推送id获取发送角色  
	 */
	@Select("SELECT r.role_name FROM t_roles r JOIN t_warning_message_role mr ON mr.role_id = r.id WHERE mr.warning_msg_id = #{id}")
	public List<String> getUsersByMsgPushId(String id);
	
	@Select("SELECT r.* FROM `t_roles` r JOIN t_user_role ur on ur.role_id=r.id  WHERE r.`status`=1 AND ur.user_id=#{userId} LIMIT 0,1")
	public Role getRoleByUserId(@Param("userId")String userId);
	
	@Select("select rs.* from t_resources rs left join t_role_resource rr on rr.resource_id = rs.id where rr.role_id = #{roleId} order by rs.resource_code asc")
	public Set<Resource> resourceList(@Param("roleId")String roleId);
	
	@Select("select r.* from t_roles r order by r.create_time desc")
	public List<Role> getAllRoles();
	
	@Insert("insert into t_user_role(user_id,role_id) values(#{userId},#{roleId})")
	public int addUserRole(@Param("userId")String userId,@Param("roleId")String roleId);
	
}
